<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/2/14
 * Time: 11:07
 */

namespace app\admin\controller;


use think\Db;

class Advertapply extends Base
{
    public function index(){
        if(request()->isAjax ()){
            $params = input('param.');
            $where = [];
            //状态筛选
            if(isset($params['adv_contact']) && $params['adv_contact'] != ''){
                $where['adv_contact'] = $params['adv_contact'];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('advert')->where($where)->count();// 获取总条数
            $lists = Db::name('advert')
                ->where($where)
                ->field('adv_id, adv_name, adv_phone, adv_company, adv_contact, adv_time')
                ->page($Nowpage,$limits)
                ->order('adv_time desc')
                ->select();
            //修改时间格式
            foreach($lists as $k => &$v){
                $v['adv_time'] = date('Y-m-d H:i', strtotime($v['adv_time']));
            }
        
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * @return \think\response\Json
     * 修改是否已经联系状态
     */
    public function changeStatus(){
        $adv_id = input('id');
        $res = Db::name('advert')->where('adv_id', $adv_id)->setField('adv_contact', 2);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' => '修改失败']);
        }
    }
    
    /**
     * @return \think\response\Json
     * 删除记录
     */
    public function advertDel(){
        $adv_id = input('id');
        $res = Db::name('advert')->delete($adv_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}